package com.thlws.study.rabbitmq.internal.helper;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.stereotype.Component;


/***
 *
 * RabbitAdmin 管理功能
 *
 * declareExchange
 * declareBinding
 * declareQueue
 *
 * deleteExchange
 * removeBinding
 * deleteQueue
 *
 * @author HanleyTang
 */
@Component
public class RabbitMqHelper {

    private final RabbitAdmin rabbitAdmin;

    public RabbitMqHelper(RabbitAdmin rabbitAdmin) {
        this.rabbitAdmin = rabbitAdmin;
    }

    public void newQueue(String queueName) {
        rabbitAdmin.declareQueue(new Queue(queueName));
    }

    public void createFanoutExchange(){

        //创建Exchange
        FanoutExchange fanoutExchange = new FanoutExchange("admin.demoFanout");
        rabbitAdmin.declareExchange(fanoutExchange);

        //创建绑定规则（queue 与 Exchange 绑定）
        Binding binding = BindingBuilder.bind(new Queue("admin.queue1")).to(fanoutExchange);
        rabbitAdmin.declareBinding(binding);

        //解除 Queue 与 Exchange 绑定
        rabbitAdmin.removeBinding(binding);

        //删除队列
        rabbitAdmin.deleteQueue("admin.queue1");

        //删除Exchange
        rabbitAdmin.deleteExchange("admin.demoFanout");
    }

    public void createFanoutExchangeII(){

        //创建Exchange(默认持久化)
        Exchange exchange=new DirectExchange("admin.demoFanout");
        rabbitAdmin.declareExchange(exchange);

        //创建Queue
        rabbitAdmin.declareQueue(new Queue("admin.queue",true));

        //创建绑定规则（queue 与 Exchange 绑定）
        rabbitAdmin.declareBinding(new Binding("admin.queue",Binding.DestinationType.QUEUE,"admin.demoFanout","admin.test",null));

    }
}
